package com.xnew.common.base;

import com.xnew.common.exception.IllegalException;
import com.xnew.common.response.BaseResponse;
import com.xnew.common.response.status.ResponseCode;
import com.xnew.common.response.status.StatusCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * @Author tinx
 * @Description 断言
 * @Date 下午 3:02 2019/3/28 0028
 * @Param
 * @return
 */
public final class Asserts {

    private Asserts() {
    }

    public static <T> void success(BaseResponse<T> result) {
        expect(result, ResponseCode.SUCCESS.getCode());
    }



    public static void success(StatusCode code) {
        success(code.getCode(), code.getMsg());
    }

    public static void success(int code, String msg) {
        if (code != ResponseCode.SUCCESS.getCode()) {
            throw new IllegalException(new StatusInfo(code, msg));
        }
    }


    public static void expectFailed(BaseResponse<?> result, StatusCode errorCode) {
        if (result.getCode()== ResponseCode.SUCCESS.getCode()) {
            throw new IllegalException(errorCode);
        }
    }


    public static void expect(BaseResponse<?> result, int expect) {
        if (result.getCode() != expect) {
            throw new IllegalException(result.getStatus());
        }
    }

    public static void expect(int code, int expect, String errMsg) {
        if (code != expect) {
            throw new IllegalException(new StatusInfo(code, errMsg));
        }
    }

    public static void successAndNotNull(BaseResponse<?> result) {
        success(result);
        valueNotNull(result);
    }

    public static void valueNotNull(BaseResponse<?> result) {
        if (result.getData() == null) {
            throw new IllegalException(result.getStatus());
        }
    }

    public static void allNotBlank(StatusCode code, Object... values) {
        int count = 0;
        for (Object item : values) {
            if (item == null) {
                count += 1;
            }
            else if (item instanceof String && StringUtils.isBlank((String)item)) {
                count += 1;
            }
            else if (item instanceof Collection && ((Collection)item).isEmpty()) {
                count += 1;
            }
            else {
                // fix sonar check result, do nothing here
            }
        }

        if (count > 0 && count == values.length) {
            throw new IllegalException(code);
        }
    }

    public static void equals(Object actual, Object expect, StatusCode errorCode) {
        if (actual == expect) {
            return;
        }
        if (actual == null) {
            throw new IllegalException(errorCode);
        }
        if (!actual.equals(expect)) {
            throw new IllegalException(errorCode);
        }
    }

    public static void notEquals(Object actual, Object expect, StatusCode errorCode) {
        if (actual == expect) {
            throw new IllegalException(errorCode);
        }
        if (actual == null) {
            throw new IllegalException(errorCode);
        }
        if (actual.equals(expect)) {
            throw new IllegalException(errorCode);
        }
    }

    /**
     * 判断是否为true
     *
     * @param bool
     * @param status
     */
    public static void isTrue(boolean bool, StatusCode status) {
        if (!bool) {
            throw new IllegalException(status);
        }
    }

    /**
     * 当传入的Object为空时
     *
     * @param o
     * @param status
     */
    public static void notNull(Object o, StatusCode status) {
        if (o == null) {
            throw new IllegalException(status);
        }
    }

    /**
     * 对象不为空
     *
     * @param object
     * @param status
     */
    public static void notEmpty(Object object, StatusCode status) {
        if (object == null) {
            throw new IllegalException(status);
        }

        if (object instanceof String && StringUtils.isEmpty((String) object)) {
            throw new IllegalException(status);
        }

        if (object instanceof Collection && ((Collection) object).isEmpty()) {
            throw new IllegalException(status);
        }
    }


}
